<?php 

namespace app\admin\validate;
//导入Validate类
use think\Validate;
use think\Db;

class Password extends Validate{
	protected $rule=[
        'password'=>'require|checkOld',
        'new'=>'require|checkRepeat|regex:[a-zA-Z0-9]{6,16}',
        're_new'=>'require|checkNew'
	];

	protected $message = [
        'password.require' => '原密码为空！',
        'password.checkOld' => '原密码不正确！',
        'new.require' => '新密码不能为空!',
        'new.checkRepeat' => '新密码不能和旧密码相同!',
        'new.regex' => '新密码格式不正确',
        're_new.require' => '确认密码不能为空',
        're_new.checkNew' => '两次密码不一致'
    ];

    public function checkOld($value, $rule, $data) 
    {
        $where = [
            'password' => $value,
            'id' => $data['id']
        ];
        $info = Db::name('user')->where($where)->find();
        
        return $info ? true : false;
    }

    public function checkRepeat($value, $rule, $data)
    {
        $where = [
            'password' => md5($value),
            'id' => $data['id']
        ];
        $info = Db::name('user')->where($where)->find();
        
        return $info ? false : true;
    }

    public function checkNew($value, $rule, $data) 
    {
        $re_new = $value;
        $new = $data['new'];

        if($re_new == $new){
            return true;
        }else{
            return false;
        }
    }

}
